<template>
  <div class='container'>
    <el-dialog
      :title="`${detail.id?'修改':'新增'}学科`"
      :visible="showDialog"
      @close="closeDialog"
      style="width: 800px; margin: 0 auto"
      class="dialog">
      <el-form
        ref="form"
        :model="formModel"
        :rules="formRules"
        label-position="left"
      >
        <el-form-item label="学科名称" prop="subjectName">
          <el-input
          v-model="formModel.subjectName"
          placeholder="请输入学科名称"
          style="display: inline-block;width: 280px;height: 32px;"
          ></el-input>
        </el-form-item>
        <el-form-item label="是否显示" prop="isFrontDisplay">
          <el-switch
            :active-value="1"
            :inactive-value="0"
            v-model="formModel.isFrontDisplay"
            active-color="#13ce66"
            inactive-color="#ff4949">
          </el-switch>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button @click="closeDialog">取消</el-button>
        <el-button type="primary" @click="confirm">确认</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import { add, update } from '@/api/hmmm/subjects.js'
export default {
  name: 'SubjectsAdd',
  props: {
    showDialog: {
      type: Boolean
    },
    detail: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data () {
    return {
      formModel: {
        id: null,
        subjectName: null,
        isFrontDisplay: 1
      },
      formRules: {
        subjectName: [
          { required: true, message: '请输入学科名称', trigger: ['blur', 'change'] },
          { min: 1, max: 30, message: '学科名称限制长度为30', trigger: ['blur', 'change'] }
        ],
        isFrontDisplay: [
          { type: 'number', message: '请选择是否显示', tirgger: ['blur', 'change'] }
        ]
      }
    }
  },
  methods: {
    closeDialog () {
      this.$emit('close-dialog')
    },
    open () {
      this.showDialog = true
      if (this.detail.id) {
        const { id, subjectName, isFrontDisplay } = this.detail
        this.formModel = { id, subjectName, isFrontDisplay }
      } else {
        this.formModel = {
          id: null,
          subjectName: null,
          isFrontDisplay: 1
        }
      }
      // 清除校验效果
      this.$nextTick(() => {
        this.$refs.form.clearValidate()
      })
    },
    async confirm () {
      this.$refs.form.validate(async valid => {
        if (valid) {
          // 提交
          if (!this.detail.id) {
            // 新增
            await add(this.formModel)
          } else {
            // 修改
            this.formModel.id = this.detail.id
            await update(this.formModel)
          }
          this.$message.success('操作成功')
          this.show = false
          this.$emit('upList')
          this.closeDialog()
        }
      })
    }
  }
}
</script>

<style scoped>
::v-deep .el-form--label-left .el-form-item__label {
  text-align: right;
}
::v-deep .el-dialog__footer {
  text-align: right;
}
::v-deep .el-input__inner {
  display: inline-block;
  height: 32px;
  width: 280px;
}

</style>
